home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / console / svgatext.3 / svgatext / SVGATextMode-1.3 / setpalette.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  6.5 KB  |  215 lines

  1. /*  SVGATextMode -- An SVGA textmode manipulation/enhancement tool
  2.  *
  3.  *  Copyright (C) 1995,1996  Koen Gadeyne
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20.  
  21. /***
  22.  *** setpalette: palette set/get program
  23.  ***/
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include "misc.h"
  30. #include "vga_prg.h"
  31. #include "messages.h"
  32. #include "string_ops.h"
  33. #include "textregs.h"
  34.  
  35. char *CommandName;
  36. bool debug_messages=FALSE;
  37. bool setreg=FALSE;
  38.  
  39. int STM_Options=0;  /* just to keep the compiler happy */
  40.  
  41. inline void printrgb(int index, int r, int g, int b, int hexdata)
  42. {
  43.   if (hexdata)
  44.     printf("0x%02x: 0x%02x 0x%02x 0x%02x\n",index,r,g,b);
  45.   else
  46.     printf("%03d: %03d %03d %03d\n",index,r,g,b);
  47. }
  48.  
  49. void dumprgb(int index, int hexdata)
  50. {
  51.   int r,g,b;
  52.   
  53.   outb(index,DAC_STATUS);
  54.   r=inb(DAC_DATA);
  55.   g=inb(DAC_DATA);
  56.   b=inb(DAC_DATA);
  57.   printrgb(index, r, g, b, hexdata);
  58. }  
  59.  
  60. void usage(int setreg)
  61. {
  62.    if (setreg)
  63.      PMESSAGE(("version %s. (c) 1995,1996 Koen Gadeyne.\n Usage: %s [options]  color_index  <R> <G> <B>\n\n\
  64.      Options: -n  Don't program VGA hardware\n\
  65.               -d  print debugging information\n\
  66.               -x  input palette values in hex instead of decimal\n\
  67.               -s  load a standard VGA palette (this doesn't need other parameters)\n\
  68.                    (use `getpalette -s' to find out what palette this is\n\
  69.               -h  print usage information\n\n\
  70.      color_index: index number in the palette look-up table (0..255).\n\
  71.                   or '-' to use standard input\n\
  72.                     (Input format = '<index>: <R-value> <G-value> <B-value>')\n\
  73.      R, G, B : color intensity for Red, Green or Blue (0..63).\n",
  74.      VERSION, CommandName));
  75.    else
  76.      PMESSAGE(("version %s. (c) 1995,1996 Koen Gadeyne.\n Usage: %s [options] <color_index>\n\n\
  77.      Options: -n  Don't program VGA hardware\n\
  78.               -d  print debugging information\n\
  79.               -x  output palette values in hex instead of decimal\n\
  80.               -s  output a standard VGA palette (this doesn't read anything from the VGA card)\n\
  81.                    (this is the palette that will be programmed when running `setpalette -s')\n\
  82.               -h  print usage information\n\n\
  83.      color_index: index number in the palette look-up table (0..255).\n\
  84.                     or 'all' to show all 256 entries\n",
  85.      VERSION, CommandName));
  86. }
  87.  
  88.  
  89. void main (int argc, char* argv[])
  90. {
  91.   int index,r,g,b;
  92.   char* commandfilename;
  93.   bool program_hardware=TRUE;
  94.   bool hexdata=FALSE;
  95.   bool stdpal=FALSE;
  96.   char c;
  97.    
  98.  /*
  99.   * See what action is required: read or write VGA register
  100.   */
  101.  
  102.   CommandName = argv[0];
  103.   commandfilename = strrchr(CommandName, '/');
  104.   if (commandfilename) commandfilename++;
  105.   else commandfilename = CommandName;
  106.   setreg = (!strncasecmp(commandfilename,"set",3));
  107.  
  108.  /*
  109.   * command-line argument parsing
  110.   */
  111.  
  112.   while ((c = getopt (argc, argv, "ndxsh")) != EOF)
  113.     switch (c)
  114.     {
  115.       case 'n': program_hardware=FALSE;
  116.                 break;
  117.       case 'd': debug_messages=TRUE;
  118.                 break;
  119.       case 'x': hexdata=TRUE;
  120.                 break;
  121.       case 's': stdpal=TRUE;
  122.                 break;
  123.       case 'h': usage(setreg);
  124.                 exit(0);
  125.                 break;
  126.       case '?': usage(setreg);
  127.                 PERROR(("Bad option `-%c'\n",(char)optopt));
  128.                 exit(-1);
  129.                 break;
  130.       default: PERROR(("getopt returned unknown token '%c'.\n",c));
  131.     }
  132.     
  133.   PVERSION;
  134.  
  135.   PDEBUG(("'%cetpalette' function selected through command name '%s'\n", (setreg) ? 's' : 'g', commandfilename));
  136.  
  137.   if (!stdpal) 
  138.   {
  139.     /* get color-index from commandline , if '-' use stdin */
  140.     if (argc<optind+1) PERROR(("Missing color index (or `%s') on commandline\n", (setreg) ? "-" : "all" ));
  141.   }
  142.  
  143.   if (!stdpal || setreg)
  144.     if (program_hardware) get_VGA_io_perm(CS_VGA);
  145.   
  146.  /*
  147.   * Start doing something useful
  148.   */
  149.  
  150.   if (stdpal)
  151.   {
  152.     if (setreg)
  153.     {
  154.       if (program_hardware)
  155.         for (index=0; index<256; index++)
  156.           WRITERGB(index,STD_PALETTE[index][0],STD_PALETTE[index][1],STD_PALETTE[index][2]);
  157.     }
  158.     else
  159.     {
  160.       if (program_hardware)
  161.         for(index=0;index<256;index++)
  162.           printrgb(index, STD_PALETTE[index][0],STD_PALETTE[index][1],STD_PALETTE[index][2], hexdata);
  163.     } 
  164.   }
  165.   else
  166.   {
  167.     if (setreg)
  168.     {  /* setpalette */
  169.     if(!strcmp(argv[optind],"-"))      /* use standard input */
  170.       {
  171.         char linebuf[1000];
  172.         int lc=0;
  173.  
  174.         while(fgets(linebuf,999,stdin))
  175.         {
  176.           lc++;
  177.           r=g=b=-1;
  178.           if (hexdata)
  179.             sscanf(linebuf,"0x%02x: 0x%02x 0x%02x 0x%02x",&index,&r,&g,&b);
  180.           else
  181.             sscanf(linebuf,"%03d: %03d %03d %03d",&index,&r,&g,&b);
  182.           if(r==-1 || b==-1 || b==-1) PERROR(("Malformed line #%d\n",lc));
  183.           if (program_hardware) WRITERGB(index,r,g,b);
  184.         }
  185.       }
  186.       else 
  187.       {
  188.         index = getint(argv[optind], "color index", 0, 255);
  189.         optind++;
  190.         if (argc<optind+1) PERROR(("Missing color value R\n"));
  191.         r = getint(argv[optind], "Color value R", 0, 63);
  192.         optind++;
  193.         if (argc<optind+1) PERROR(("Missing color value G\n"));
  194.         g = getint(argv[optind], "Color value G", 0, 63);
  195.         optind++;
  196.         if (argc<optind+1) PERROR(("Missing color value B\n"));
  197.         b = getint(argv[optind], "Color value B", 0, 63);
  198.         PDEBUG(("Color index = %d (0x%x); Color values : R = %d (0x%x) , G = %d (0x%x) , B = %d (0x%x).\n", index, index, r, r, g, g, b, b));
  199.    
  200.         if (program_hardware) WRITERGB(index,r,g,b);
  201.       }
  202.     }
  203.     else
  204.     {  /* getpalette */
  205.       if(strcasecmp(argv[optind],"all"))
  206.       {
  207.         index = getint(argv[optind], "color index", 0, 255);
  208.         if (program_hardware) dumprgb(index, hexdata);
  209.       }
  210.       else 
  211.         if (program_hardware) for(index=0;index<256;index++) dumprgb(index, hexdata);
  212.     }
  213.   }
  214. }
  215.